package com.example.myapplication.service;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import androidx.annotation.Nullable;

import com.example.myapplication.util.FileHelper;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class DownloadService extends IntentService {

    /**
     * @param
     * @deprecated
     */
    public DownloadService() {
        super("name");
        Log.i( "DemoLog" , "DownloadIntentService构造函数, Thread: " + Thread.currentThread().getName());
    }

    /**
     * 耗时操作
     * @param intent
     */
    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        HttpURLConnection conn = null ;
        InputStream is = null ;
        String blogUrl = intent.getStringExtra( "url" );
        String blogName = intent.getStringExtra( "name" );
        URL url = null;
        try {
            url = new URL(blogUrl);
            conn = (HttpURLConnection) url.openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(conn != null){}
        // todo 文件批量下载耗时任务编写
        /*try {
            //下载指定的文件
            //URL url = new URL(blogUrl);
            //conn = (HttpURLConnection)url.openConnection();
            if (conn != null ){
                //我们在此处得到所下载文章的输入流，可以将其以文件的形式写入到存储卡上面或
                //将其读取出文本显示在App中
                //is = conn.getInputStream();
            File file = new File("D:\\a.txt");
            File file1 = new File("D:\\b.txt");
            is = new FileInputStream(file);
                FileOutputStream fos = new FileOutputStream(file1);
                byte[] b = new byte[1024];
                while ((is.read(b)) != -1) {
                    fos.write(b);// 写入数据
                }
            }
        } catch (MalformedURLException e){
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null ){
                conn.disconnect();
            }
        }*/
        Log.i( "DemoLog" , "DownloadIntentService -> onHandleIntent, Thread: " + Thread.currentThread().getName() + ", 《" + blogName + "》下载完成" );
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i( "DemoLog" , "DownloadIntentService -> onCreate, Thread: " + Thread.currentThread().getName());
    }

    @Override
    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
        Log.i( "DemoLog" , "DownloadIntentService -> onStartCommand, Thread: " + Thread.currentThread().getName() + " , startId: " + startId);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i( "DemoLog" , "DownloadIntentService -> onDestroy, Thread: " + Thread.currentThread().getName());
    }


    public static void main(String[] args) throws Exception{
        String pathname = "D://a.txt"; // 绝对路径或相对路径都可以，这里是绝对路径，写入文件时演示相对路径
        File filename = new File(pathname); // 要读取以上路径的input。txt文件
        InputStreamReader reader = new InputStreamReader(
                new FileInputStream(filename)); // 建立一个输入流对象reader
        BufferedReader br = new BufferedReader(reader); // 建立一个对象，它把文件内容转成计算机能读懂的语言
        String line = "";
        line = br.readLine();
        while (line != null) {
            line = br.readLine(); // 一次读入一行数据
        }
        System.out.println(line);
    }
}
